import time

from selenium import webdriver
from selenium.webdriver.common.by import By


class EleOperation:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.baidu.com')

    def quit(self,second=3):
        time.sleep(second)
        self.driver.quit()

    # 通过id属性定位
    def id(self):
        kw = self.driver.find_element(By.ID,'kw')
        # get_attribute() 获取元素属性的方法
        classname = kw.get_attribute('class')
        print(classname)
        # outerHTML来获取元素的html代码
        print(kw.get_attribute('outerHTML'))

    # 通过class属性定位
    def class_name(self):
        s_ipt = self.driver.find_element(By.CLASS_NAME,'s_ipt')
        print(s_ipt.get_attribute('outerHTML'))

    # 通过链接文本定位
    def link_text(self):
        news = self.driver.find_element(By.LINK_TEXT,'新闻')
        print(news.get_attribute('outerHTML'))

    # 通过部分链接文本定位
    def partial_link_text(self):
        hao123 = self.driver.find_element(By.PARTIAL_LINK_TEXT,'hao')
        print(hao123.get_attribute('outerHTML'))

    # 通过name属性定位
    def name(self):
        wd = self.driver.find_element(By.NAME,'wd')
        print(wd.get_attribute('outerHTML'))

    # 通过tag name(标签名) 属性定位
    def tag_name(self):
        inputs = self.driver.find_elements(By.TAG_NAME,'input')
        print(inputs[0].get_attribute('outerHTML'))
        print('-'*60)
        for i in inputs:
            print(i.get_attribute('outerHTML'))

    #  找不到元素时的报错
    def about_error(self):
        kw = self.driver.find_element(By.ID, 'abc')
        print(kw.get_attribute('outerHTML'))


if __name__ == '__main__':
    eo = EleOperation()
    # eo.id()
    # eo.class_name()
    # eo.link_text()
    # eo.partial_link_text()
    # eo.name()
    # eo.tag_name()
    # time.sleep(100)
    # eo.about_error()
    eo.quit()
